
class ImagesShackPlugin:
	def __init__(self):
		# put all the links this plugins supports here - it will be used to filter supported URLs
		self.supported_sites = ['imageshack.us']
		self.supported_urls = self.supported_sites

	def get(self, url):
		"""Get the file from the url"""
		if not urlparse.urlparse(url)[2].startswith('/img'): # direct link
			prefix = '/' + urlparse.urlparse(url)[1].split('.')[0]
			parser = URLLister()
			parser.feed(urllib2.urlopen(url).read())
			for img_url in parser.image_urls:
				if img_url.find('imageshack.us') != -1:
					parsed_url = urlparse.urlparse(img_url)
					if parsed_url[2].startswith(prefix) and parsed_url[2].find('http://') == -1 and not img_url.endswith('.th.jpg'):
						url = img_url
						break
			parser.close()

		local_file = get_filename_from_url(url)
		download_file_with_retries(local_file, url)
		config.report.report_finish(local_file)

	def url_matches(self, url):
		"""Check whether we support this URL"""
		if url.endswith('.th.jpg'):
			return False
		for l in self.supported_urls:
			if url.find(l) != -1:
				return True
		return False

	def version(self):
		"""Return plugin version - when interface changes, the version changes"""
		return 1

modules = [ImagesShackPlugin()]
